#include <stdlib.h>
#include "linkStack.h"

int getLinkStackLengthi(linkstacki *plinkstack)
{
    return plinkstack->length;
}

int isEmptyLinkStacki(linkstacki *plinkstack)
{
    if(plinkstack->length == 0 && plinkstack->pitop == NULL)
    {
        return 1;
    }
    else if(plinkstack->length != 0 && plinkstack->pitop != NULL)
    {
        return 0;
    }
    else
    {
        return -1;
    }
}

int pushLinkStacki(linkstacki *plinkstack, int item)
{
    if(isEmptyLinkStacki(plinkstack) == 1 )
    {
        plinkstack->pitop = (linknodei*)calloc(sizeof(linknodei),1);
        if(plinkstack->pitop == NULL)
        {
            return -1;
        }
        plinkstack->pitop->value = item;
        plinkstack->pitop->pnext = NULL;
        plinkstack->length++;
        return 0;
    }
    else if(isEmptyLinkStacki(plinkstack) == 0)
    {
        linknodei *poldtop = plinkstack->pitop;
        plinkstack->pitop = (linknodei*)calloc(sizeof(linknodei),1);
        if(plinkstack->pitop == NULL)
        {
            return -1;
        }
        plinkstack->pitop->value = item;
        plinkstack->pitop->pnext = poldtop;
        plinkstack->length++;
        return 0;
    }
    else
        return -1;
}

int popLinkStacki(linkstacki *plinkstack)
{
    if(isEmptyLinkStacki(plinkstack) == 1)
    {
        int popvalue = plinkstack->pitop->value;
        linknodei *poldtop = plinkstack->pitop;
        plinkstack->pitop = plinkstack->pitop->pnext;
        plinkstack->length--;
        free(poldtop);
        return popvalue;
    }
    else
    {
        return -1;
    }
}
